package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Activity;
import com.example.demo.entity.ActivitySign;
import com.example.demo.entity.Appointment;
import com.example.demo.mapper.ActivitySignMapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * @descreption:
 * @author: xhr
 * @date: 2022/9/29
 */
@RestController
@RequestMapping("/activitySign")
public class ActivitySignController {
    @Resource
    ActivitySignMapper activitySignMapper;

    //增
    @PostMapping
    public Result<?> save(@RequestBody ActivitySign activitySign) {

        ActivitySign res=activitySignMapper.selectOne(new QueryWrapper<ActivitySign>().eq("user_id", activitySign.getUserId()).eq("activity_id",activitySign.getActivityId()));
        if (res == null) {
            activitySignMapper.insert(activitySign);
            return Result.success();
        }else {
            return Result.error("-1","已报名!");
        }

    }
     //改
    @PutMapping
    public Result<?> update(@RequestBody ActivitySign activitySign) {
        activitySignMapper.updateById(activitySign);
        return Result.success();
    }
    //批量删除
    @DeleteMapping("/delBatch/{ids}")
    public Result<?> delAll(@PathVariable("ids") Integer[] ids) {
        List<Integer> list = Arrays.asList(ids);
        activitySignMapper.deleteBatchIds(list);
        return Result.success();
    }
    // 删
    @DeleteMapping("/{id}")
    public Result<?> update(@PathVariable Long id) {
        activitySignMapper.deleteById(id);
        return Result.success();
    }
    //给某个用户看活动报名情况,需要一表多查
    @GetMapping("/page/front/{userId}")
    public Result<?> selectActivity(@PathVariable Integer userId) {
        List<Activity> list=activitySignMapper.selectActivity(userId);
        return Result.success(list);
    }
    //全(页)查
    @GetMapping("/page")
    public Result<?> findPage(@RequestParam(defaultValue = "10") Integer pagesize,
                              @RequestParam(defaultValue = "1") Integer pagenum) {
        LambdaQueryWrapper<ActivitySign> wrapper = Wrappers.<ActivitySign>lambdaQuery();

        return Result.success(activitySignMapper.selectPage(new Page<>(pagenum, pagesize), wrapper));
    }

}
